package com.bigzero.workflow.modules.system.controller;

import com.bigzero.workflow.common.exception.WorkflowException;
import com.bigzero.workflow.common.model.R;
import com.bigzero.workflow.common.model.TableDataInfo;
import com.bigzero.workflow.common.utils.PageUtil;
import com.bigzero.workflow.modules.system.entity.DeptEntity;
import com.bigzero.workflow.modules.system.query.DeptQuery;
import com.bigzero.workflow.modules.system.service.DeptService;
import com.bigzero.workflow.modules.system.vo.DeptVo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.*;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 部门信息维护
 *
 * @author fanxinxiong
 * @date 2025-02-08 17:52
 **/
//@Tag(name = "DeptController", description = "系统管理-->部门管理")
@ApiSupport(order = 1)
@Tag(name = "系统管理-部门管理", description = "系统管理-部门管理")
@RequestMapping("/system/dept")
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    /**
     * 列表
     * @param query 参数
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 1)
    @Operation(summary = "分页列表")
    /*
    @Parameters({
        @Parameter(name = "dto", description = "部门实体", hidden = true),
        @Parameter(name = "deptName", description = "部门名称", schema = @Schema(type = "string"), required = false)
    })*/
    @Parameters({
        @Parameter(name = "pageNo", description = "当前页", schema = @Schema(type = "Integer"), example="1", required = true),
        @Parameter(name = "pageSize", description = "每页显示记录数", schema = @Schema(type = "Integer"), example="10", required = true)
    })
    @ApiResponses(value = {
        //@ApiResponse(responseCode = "200", description = "成功响应", content = @Content(mediaType = "application/json", schema = @Schema(implementation = DeptVo.class)))
        @ApiResponse(responseCode = "200", description = "成功响应", content = @Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = DeptVo.class))))
    })
    @PreAuthorize("hasAuthority('system:dept:list')")
    @GetMapping("list")
    public TableDataInfo list(DeptQuery query) {
        PageUtil.startPage();
        List<DeptVo> list = deptService.queryPage(query);
        return PageUtil.getDataTable(list);
    }

    /**
     * 详情
     * @param deptId 部门id
     * @return 部门详情
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 2)
    @Operation(summary = "详情")
    @Parameters({
       @Parameter(name = "deptId", description = "部门id", example = "gf45e0320d93832c90dd3ed323129dd3", required = true)
    })
    @GetMapping("info/{deptId}")
    public R<DeptEntity> info(@PathVariable String deptId) {
        DeptEntity model = deptService.getById(deptId);
        return R.ok(model);
    }

    /**
     * 保存
     * @param model 数据
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 3)
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('system:dept:insert') or hasAuthority('system:dept:update')")
    @PostMapping("save")
    public R<String> save(@RequestBody DeptEntity model) {
        if (StringUtils.isNotEmpty(model.getId())) {
            DeptEntity dept = deptService.getById(model.getId());
            if (dept == null) throw new WorkflowException("未知数据");
            if (dept.getIsSys() == 1) throw new WorkflowException("系统内置不能修改!");
        }

        Date date = new Date();
        if(StringUtils.isEmpty(model.getId())) {
            model.setCreateTime(date);
        }
        model.setUpdateTime(date);
        deptService.saveOrUpdate(model);
        return R.ok();
    }

    /**
     * 删除
     *
     * @param id 主键
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 4)
    @Operation(summary = "删除")
    //@Parameters({
    //   @Parameter(name = "id", description = "部门id", example = "1", schema = @Schema(type = "string"), required = true)
    //})
    @PreAuthorize("hasAuthority('system:dept:delete')")
    @DeleteMapping("delete")
    public R<String> delete(@RequestBody @Schema(description = "部门id", example = "1", required = true) String id) {

        DeptEntity dept = deptService.getById(id);
        if(dept == null) throw new WorkflowException("未知数据");
        if(dept.getIsSys() == 1) throw new WorkflowException("系统内置不能删除!");

        deptService.removeById(id);
        return R.ok();
    }

    /**
     * 部门列表
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 5)
    @Operation(summary = "部门列表")
    @ApiResponses(value = {
       //@ApiResponse(responseCode = "200", description = "成功响应", content = @Content(mediaType = "application/json", schema = @Schema(implementation = R.class)))
       //@ArraySchema 定义集合内的元素类型
       @ApiResponse(responseCode = "200", description = "成功响应",
         content = @Content(
           //mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = DeptEntity.class))
           mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = DeptEntity.class))
         ))
    })
    @GetMapping("deptList")
    public R<List<DeptEntity>> list() {
        List<DeptEntity> list = deptService.list();
        return R.ok(list);
    }

}
